<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\WxAttachment;
use App\Jobs\Attachment\VideoCosReviewSubmit;
use App\Wen\Utils\Settings;
use App\Wen\Utils\StrUtils;
use App\Wen\Utils\Utils;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxAttachmentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxAttachment(), function (Grid $grid) {
            $grid->simplePaginate();
            $grid->quickSearch(['id', 'user_id', 'path'])->placeholder('搜索id, 用户id，路径名...')->width(35);
            $grid->column('id')->sortable();
            $grid->column('users', '头像')->display(function ($v) {
                if($this->user['user_avatar'] ?? '') {
                    return '<img src="' . ($this->user['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png')) . '"  style="border-radius:50px;width:30px;" />';
                }
                return '';
            })->width('50px');
            $grid->column('user')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('/users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '';
            })->width('100px');
//            $grid->column('path')->image('',80,200);
            $grid->column('paths', '路径')->display(function () {
                return  '<a target="_blank" href="'. $this->domain . '/' . $this->path .'">'. $this->domain . '/' . $this->path . '</a>';
            });
            $grid->column('show', '路径')->display(function ($v) {
                $url =  $this->domain . '/' . $this->path;
                if(StrUtils::endsWith($url, '.mp4')){
                    $file_type = 'video';
                }else if(StrUtils::endsWith($url, ['.jpg', '.png', 'jpeg', '.webp', '.gif'])){
                    $file_type = 'image';
                }else if(StrUtils::endsWith($url, ['.mp3'])){
                    $file_type = 'audio';
                }else{
                    $file_type = 'other';
                }
                if(in_array($file_type, ['video', 'image', 'audio'])){
                    if($file_type == 'image'){
                        return '<a target="_blank" href="'. $url .'"><img src="'. Utils::imgWithStyle($url, 2) . '" style="max-height: 100px;"/></a>';
                    }else if($file_type == 'audio'){
                        return '<audio controls><source src="'.$url.'" type="audio/mp3"></audio>';
                    }else if($file_type == 'video'){
                        return '<video controls width="150">
  <source src="'.$url.'" type="video/mp4">
  您的浏览器不支持 video 标签。
</video>';
                    }
                    return '';
                }
            });
            $grid->column('store_type','保存位置');
            $grid->column('created_at');

            $grid->column('cos_review')->using([0 => '正常', 1 => '确认违规', 2=> '疑似违规', 3=>'未审', 4=>'送审中', 5=>'待查询结果'])->label([
                0 => '#5ac725',
                1 => '#f56c6c',
                2 => '#f9ae3d',
                3 => 'gray',
                4 => 'gray',
                5 => 'gray'
            ])->display(function ($v){
                if($this->cos_review == 4){
                    VideoCosReviewSubmit::dispatch($this->id);
                }
                return $v;
            })->sortable();

            $grid->column('state', '附件状态')->using([0 => '未引用', 1 => '已引用', 2=> '已清理', 3=>'cos冻结中', 4=>'cos已转移'])->label([
                0 => 'red',
                1 => 'primary',
                2 => 'default',
                3 => '#f9ae3d',
                4 => '#f9ae3d'
            ])->sortable();
//            $grid->column('updated_at');
            $grid->model()->orderBy('id', 'desc');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('user_id');
            });
//            $grid->disableActions();
            $grid->disableViewButton();
            $grid->disableEditButton();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxAttachment(), function (Show $show) {
            $show->field('id');
            $show->field('path');
            $show->field('cos_review');
            $show->field('created_at');
            $show->field('updated_at');
            $show->disableEditButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxAttachment(), function (Form $form) {
            $form->display('id');
            $form->text('path');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
